<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/09/03
 * Time: 14:58
 */

namespace business\datacenter\repositories;

use business\company\models\SupplierBasicModel;
use yii\db\Query;

class SupplierRecruitRepository
{
    /**
     * 数据连接
     * @var \yii\db\Connection
     */
    private $db;

    public function __construct()
    {
        $this->db = \Yii::$app->db;
    }

    /**
     * 通过供应商ID获取招募报名分类信息
     * @param string $supplierId
     * @param string $fields
     * @return array|\yii\db\Command
     */
    public function getRecruitItemBySupplierId
    (
        $supplierId = '',
        $fields = 'r.item_id, f.`category_name` as `name`, r.created_time as register_time'
    ) {
        if (!empty($supplierId)) {
            return (new Query())
                ->from('`b2b_bidding`.`bid_recruit_item_register` r')
                ->select($fields)
                ->innerJoin('`b2b_bidding`.`bid_recruit_item` i', 'r.item_id = i.item_id')
                ->innerJoin('`b2b_bidding`.`bid_recruit_item_categories` c', 'i.item_id = c.item_id')
                ->innerJoin('`b2b_parameter`.`bsp_basic_forest` f', 'c.category_code = f.category_code')
                ->innerJoin(SupplierBasicModel::tableName() . ' a', 'a.supplier_id = r.supplier_id')
                ->where(['r.supplier_id' => $supplierId, 'f.category_status' => 1])
                ->andWhere(['<>', 'r.register_state', 3])
                ->andWhere(['NOT LIKE', 'a.company_name', 'test'])
                ->createCommand($this->db)
                ->queryAll();
        }

        return [];
    }
}
